import Cookies from "js-cookie";

export function isDevelopment() {
  return process.env.NODE_ENV == 'development'
}


//是否为oss环境
export function isOss() {
  return Cookies.get('oss') == '1'
}

//链式调用取值
export function v(obj, objKey) {
  const keyList = objKey.split('.');
  return keyList.reduce((pre, item) => pre[item], obj);
}

//获取文件服务器预览地址
export function getPreviewUrl(fileName) {
  // 获取图片封面
  if (!fileName) {
    return require("@/assets/401_images/401.gif")
  } else {
    return 'https://' +  process.env.VUE_APP_OSS_FILE_URL + '/' + fileName; //OSS 缩略图规则
  }
}


//价格精度为0
export const priceStoreDigits = 0
export const priceFixedDigits = 4

export function priceFormatter(price, displayUnit = '$', fixedDigits = priceFixedDigits, storeDigits = priceStoreDigits) {
  return `${(price / 10 ** storeDigits).toFixed(2)} ${displayUnit}`
}

//存储价格转为显示价格
export function showPrice(price, storeDigits = priceStoreDigits) {
  return price / 10 ** storeDigits
}


//显示价格转为存储价格
export function storePrice(price, storeDigits = priceStoreDigits) {
  return price * 10 ** storeDigits
}


//判断dom内文字的行数
export function getLineTotal(el) {
  let styles = window.getComputedStyle(el, null);
  let totalHeight = styles.height.replace('px', '');
  let lineHeight = styles.lineHeight.replace('px', '');
  return Math.ceil(totalHeight / lineHeight);
}

export function stringIsNotEmpty(str) {
  return str != null && str != undefined && str.trim().length > 0
}

//通过条件删除数组中的符合条件的第一个对象
export function listRemoveForCondition(list, condition) {
  const index = list.findIndex(i => condition(i))
  if (index != null) {
    list.splice(index, 1)
  }
  return list
}
//提示功能暂未开发
export function handleTodo(){
  window.alert("待开发");
}
//带零时区时间格式转换
export function convertTimestamp(timestamp) {
  if (typeof timestamp == 'string') timestamp = parseInt(timestamp)
  var date = new Date(timestamp);
  var year = date.getFullYear();
  var month = ("0" + (date.getMonth() + 1)).slice(-2);
  var day = ("0" + date.getDate()).slice(-2);
  var hours = ("0" + date.getHours()).slice(-2);
  var minutes = ("0" + date.getMinutes()).slice(-2);
  var seconds = ("0" + date.getSeconds()).slice(-2);

  var formattedDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  if (formattedDate === '1970-01-01 00:00:00' || formattedDate === '1970-01-01 08:00:00') {
    return '暂无时间';
  }
  return formattedDate;
}

export function convertToZeroUTC(timestamp) {
  var timezoneOffset = new Date().getTimezoneOffset();
  var zeroUTC = timestamp + timezoneOffset * 60 * 1000;
  return zeroUTC;
}
// 金额格式化
export function formatAmount(amount) {
  if (amount === undefined || amount === null) {
    amount = 0;
  }
  return amount.toFixed(2).replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
}

// 删除对象中的null
export function removeEmptyValues(obj) {
  Object.entries(obj).forEach(([key, value]) => {
    if (value === null || value === undefined || value === '') {
      delete obj[key];
    }
  });
}


export const FileEnum = {
  USER_TOP_IMG: "user_top_img",
  SYS_USER_TOP_IMG: "sys_user_top_img",
  SHOP_TOP_IMG: "shop_top_img",
  SHOP_SIGN_IMG: "shop_sign_img",
  PRODUCT_GOODS_IMG: "product_goods_img",
  SHORT_VIDEO: "short_video",
  SYS_OTHER: "sys_other",
}
